<template>
  <div class="pair-container">
    <div class="pair-header">
      <div class="search">
        <el-form :inline="true">
          <el-form-item label="流程卡号" label-width="72px">
            <el-input placeholder="请输入流程卡号(未配对)" prefix-icon="el-icon-search" v-model="cardcode" size="mini" clearable
              @change="initData">
            </el-input>
          </el-form-item>
          <el-form-item label="类型" label-width="72px">
            <el-select v-model="outRule" placeholder="请选择类型" size="mini" @change="initData" clearable>
              <el-option v-for="item in  options " :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div>

        </div>
        <div class="btnSearch">
          <div class="label">小车状态:</div>
          <el-radio-group v-model="glassOut" @change="handlerOut()">
            <el-radio :label="true">允许发送夹胶下片台</el-radio>
            <el-radio :label="false">关闭发送夹胶下片台</el-radio>
          </el-radio-group>
        </div>
      </div>
    </div>
    <div class="view">
      <div class="left_view">
        <el-table :data="rwdList" border height="100%">
          <el-table-column label="流程卡任务号" align="center" key="rwdidno" prop="rwdidno" width="100">
            <template slot-scope="scope">
              <div class="rwdidno_text" @click="handlerOpenDialog(scope.row)">
                {{ scope.row.rwdidno }}
              </div>
            </template>
          </el-table-column>

          <el-table-column label="流程卡号" align="center" key="cardcode" prop="cardcode" width="80" />
          <el-table-column label="生产数" align="center" key="quantity" prop="quantity" fit width="60"
            show-overflow-tooltip />
          <el-table-column label="可配对数" align="center" key="pairedquantity" prop="pairedquantity" fit width="60"
            show-overflow-tooltip />
          <el-table-column label="在线数" align="center" key="deliveredquantity" prop="deliveredquantity" fit width="60"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="最大宽度" align="center" key="maxwidth" prop="maxwidth" fit width="80" />
          <el-table-column label="最大高度" align="center" key="maxheight" prop="maxheight" fit width="80"
            show-overflow-tooltip />
          <el-table-column label="片标记" align="center" key="tag" prop="tag" fit width="60" />

          <el-table-column label="出片规则" align="center" key="outrule" prop="outrule" />
          <el-table-column label="是否为夹胶" align="center" key="outRule">
            <template slot-scope="scope">
              <el-tag :type="scope.row.outrule.charAt(0) == '-' ? 'success' : 'warning'">
                {{ scope.row.outrule.charAt(0) == '-' ? '夹胶' : '中空' }}
              </el-tag>
            </template>

          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="handlerAddCard(scope.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="Text">未配对</div>
      </div>
      <div class="right_view">
        <el-table :data="rwdidNoList" border height="100%">
          <el-table-column label="流程卡任务号" align="center" key="rwdidno" prop="rwdidno" width="120">
            <template slot-scope="scope">
              <div class="rwdidno_text" @click="handlerOpenDialog(scope.row)">
                {{ scope.row.rwdidno }}
              </div>
            </template>
          </el-table-column>

          <el-table-column label="流程卡号" align="center" key="cardcode" prop="cardcode" width="90" />
          <el-table-column label="生产数" align="center" key="quantity" prop="quantity" width="70" show-overflow-tooltip />
          <el-table-column label="在线数" align="center" key="deliveredquantity" prop="deliveredquantity" width="50"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="最大宽度" align="center" key="maxwidth" prop="maxwidth" width="70" />
          <el-table-column label="最大高度" align="center" key="maxheight" prop="maxheight" width="70"
            show-overflow-tooltip />
          <el-table-column label="片标记" align="center" key="tag" prop="tag" />
          <el-table-column label="出片规则" align="center" key="outrule" prop="outrule" width="70" />
          <el-table-column label="状态" align="center" key="outState" prop="outState" width="60">
            <template slot-scope="scope">
              {{ scope.row.outState | status }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template>
              <el-button type="text" @click="handlerAddCard({ cardcode: '' })">移除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="Text">正在取片</div>
      </div>
    </div>
    <div class="note">
      提示： 如果允许发送夹胶台,A2车将不会取钢化后立式输送台上的玻璃
    </div>
    <el-dialog :title="selectRwdId + '流程卡在线玻璃'" :visible.sync="dialogOpen" width="50%">
      <el-table :data="lineExitData" border height="600px" v-loading="loadingDialogTable"
        :row-class-name="tableRowClassName">
        <el-table-column label="ID" align="center" key="id" prop="id">
        </el-table-column>

        <el-table-column label="类型" align="center" key="type" prop="type" show-overflow-tooltip />
        <el-table-column label="宽度" align="center" key="width" prop="width" show-overflow-tooltip />
        <el-table-column label="高度" align="center" key="height" prop="height" fit show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="客户名称" align="center" key="clientName" prop="clientName" fit show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="玻璃位置" align="center" fit show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.quartz_common_location" :value="scope.row.place" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" key="state" fit show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag :type="scope.row.id &&
          scope.row.place == 'ZKFrontCage' &&
          scope.row.state == '0'
          ? 'success'
          : 'danger'
          ">
              {{
          scope.row.id &&
            scope.row.place == "ZKFrontCage" &&
            scope.row.state == "0"
            ? "✔已到位"
            : "×未到位"
              }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDoublingRwdInfo,
  editBehindCageParam,
  operateRwdLine,
  getRwdGlass,
  getUseDoublingRwdInfo,
} from "@/api/assembly/behindCage.js";
export default {
  dicts: [
    "quartz_glass_state",
    "quartz_common_location_search",
    "quartz_common_location",
  ],
  data() {
    return {
      timer: null,
      rwdList: [],
      dialogOpen: false,
      lineExitData: [],
      selectRwdId: "",
      loadingDialogTable: false,
      cardcode: '',
      rwdidNoList: [],
      glassOut: null,
      options: [{
        label: '中空',
        value: 0
      }, {
        label: "夹胶",
        value: 1
      }],
      outRule: ''
    };
  },
  methods: {
    handlerOut(value) {
      editBehindCageParam({
        object: { postPvb_A443: this.glassOut },
      }).then((res) => {
        this.$message.success(res.msg || "操作成功");
        this.initData();
      });
    },
    handlerOutGlass(isOut) {
      this.$confirm("此操作将选中的流程卡号进行操作, 是否继确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          editBehindCageParam({
            object: { postPvb_A443: isOut },
          }).then((res) => {
            this.$message.success(res.msg || "操作成功");
            this.initData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.id && row.place == "ZKFrontCage" && row.state == "0") {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    handlerOpenDialog(e) {
      this.selectRwdId = e.rwdidno;
      this.dialogOpen = true;
      this.loadingDialogTable = true;
      let params = {
        id: e.id,
        rwdidno: e.rwdidno,
      };
      getRwdGlass(params).then((res) => {
        this.lineExitData = res.data;
        this.loadingDialogTable = false;
      });
    },
    handlerPairGlass(item, e) {
      let data = {
        id: item.id,
        operateType: e,
      };
      operateRwdLine(data).then((res) => {
        this.$message.success("操作成功");
        this.initData();
        this.getLineProductCard();
      });
    },
    handlerAddCard(item) {
      this.$confirm("此操作将选中的流程卡号进行取片, 是否继确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          editBehindCageParam({
            object: { originalCard: item.cardcode },
          }).then((res) => {
            this.$message.success(res.msg || "操作成功");
            this.initData();
            this.getLineProductCard();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    initData() {
      getDoublingRwdInfo().then((res) => {
        this.rwdList = res.data.filter((item) => this.cardcode == '' || item.cardcode.includes(this.cardcode)).filter((item) => {
          if(this.outRule == '') return true
          if (this.outRule == 1) {
            return item.outrule.charAt(0) == '-' ? true : false
          } else {
            return  item.outrule.charAt(0) == '-' ? false : true
          }
        });
      });
    },
    getLineProductCard() {
      getUseDoublingRwdInfo().then((res) => {
        this.rwdidNoList = res.data.masterList;
        this.glassOut = res.data.postA443;
      });
    },
  },
  computed: {
    ruleData() {
      let map = {};
      this.rwdList.forEach((item) => {
        if (map[item.rwdidno]) {
          map[item.rwdidno].num++;
        } else {
          map[item.rwdidno] = {};
          map[item.rwdidno].num = 1;
          map[item.rwdidno].maxNum = item.outrule
            .split("-")
            .filter((item) => item !== "").length;
          map[item.rwdidno].isProd = true;
        }
        if (item.quantity != item.pairedquantity) {
          map[item.rwdidno].isProd = false;
        }
      });
      return map;
    },
  },
  filters: {
    status(arg) {
      return arg ? "出片" : "等待";
    },
  },
  created() {
    clearInterval(this.timer);
    this.initData();
    this.getLineProductCard();
    this.timer = setInterval(() => {
      this.initData();
      this.getLineProductCard();
    }, 10000);
  },
  mounted() { },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-table {
  .warning-row {
    background: oldlace;
  }

  .success-row {
    background: #f0f9eb;
  }

  .cell {
    padding: 0 !important;
    font-size: 14px;
  }
}

/deep/ .el-form-item {
  margin-bottom: 0;
}

/deep/ .el-dialog__body {
  padding: 10px 20px !important;
}

.btnSearch {
  height: 100%;
  margin-left: auto;
  display: flex;
  align-items: center;

  .label {
    display: flex;
    align-items: center;
    font-size: 14px;
    height: 100%;
    padding: 0 5px 0 15px;
  }

  /deep/ .el-radio {
    margin-left: 10px;
    margin-right: 10px;
    transform: translateY(1px);
  }
}

.pair-container {
  height: 100%;
  padding: 0 10px 0px 10px;
  display: flex;
  flex-direction: column;

  .pair-header {
    min-height: 35px;
    display: flex;
    border-bottom: 1px solid #e6e6e6;

    .search {
      width: 100%;
      display: flex;
      align-items: center;
    }

    .radioSelect {
      margin-left: auto;
      display: flex;

      .silder {
        height: 34px;
        min-width: 100px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 18px;
        font-weight: bold;
        cursor: pointer;
        transform: translateY(1px);
      }
    }

    .bottom-line {
      // border-bottom: 2px solid #409eff;
      background: linear-gradient(#fafafa, #1890ff);
    }

    .silder:hover {
      border-bottom: 2px solid #409eff;
    }

    .recument-no {
      width: 100%;
      height: 50px;
    }

    .tips {
      height: 100%;
      display: flex;
      align-items: center;
      margin-left: auto;
      margin-right: 20px;
      letter-spacing: 1.5px;
      font-size: 16px;
    }
  }

  .view {
    max-height: calc(100vh - 110px);
    flex: 1;
    display: flex;

    .left_view {
      position: relative;
      width: 50%;
      height: 100%;
      border-left: 1px solid #e6e6e6;
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      justify-content: space-between;
      overflow: auto;
      border-right: 1px solid #e6e6e6;

      .rwdidno_text {
        color: #1890ff;
      }

      .rwdidno_text:hover {
        text-decoration: underline;
        color: #1890ff;
        cursor: pointer;
      }

      .card {
        height: 48%;
        width: 220px;

        padding: 5px;
        color: #fafafa;
        display: flex;
        flex-direction: column;
        margin-right: 10px;

        .item {
          display: flex;
          align-items: center;
          font-size: 16px;
          font-weight: bold;
          margin-bottom: 8px;

          .value {
            margin-left: auto;
          }
        }
      }

      .active {
        border: 3px solid red;
      }

      .Text {
        position: absolute;
        bottom: 5px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 24px;
        font-weight: bold;
        letter-spacing: 4px;
        color: #c1c1c1;
      }
    }

    .right_view {
      position: relative;
      width: 50%;
      height: 100%;
      border-right: 1px solid #e6e6e6;
      display: flex;
      flex-wrap: wrap;
      overflow: auto;

      .card {
        height: 48%;
        width: 207px;
        padding: 5px;
        color: #fafafa;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-right: 10px;

        .item {
          display: flex;
          align-items: center;
          font-size: 16px;
          font-weight: bold;

          .value {
            margin-left: auto;
          }
        }
      }

      .Text {
        position: absolute;
        bottom: 5px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 24px;
        font-weight: bold;
        letter-spacing: 4px;
        color: #c1c1c1;
      }
    }

    .divider {
      position: relative;

      .icon {
        position: absolute;
        padding: 15px;
        right: 50%;
        top: 50%;
        transform: translate(50%, -100%);
        background-color: #fff;
        font-size: 40px;
        cursor: pointer;
      }

      .icon:hover {
        color: #1890ff;
      }

      /deep/ .el-divider--vertical {
        height: 100%;
      }
    }
  }

  .note {
    font-size: 13px;
    color: #515a6e;
  }
}
</style>
